import { getcommunity, getbuilding, getunit, gethouse, postuserhouse, putuser } from "../../utils/api"
import { setStorageSyncSecond, getStorageSyncTime } from "../../utils/storage"
import '../../utils/lodash_fix'
let _ = require("../../utils/lodash.min");
const to = require('../../utils/jump')
Page({
  data: {
    namevalue: '',
    phone: '',
    areashow: false,
    arealist: [],
    areaValue: '',
    areaid: '',
    houseValue: '',
    houselist: [],
    houseshow: false,
    unitshow: false,
    unitlist: [],
    unitValue: '',
    roomshow: false,
    roomlist: [],
    roomValue: '',
    roomid: '',
  },
  onLoad(e) {
    if (e.first) {
      wx.showModal({
        showCancel: false,
        title: '绑定房屋',
        content: '点击确定，开始绑定房屋',
        success: res => {
          if (res.confirm) {
            this.setData({ areashow: true })
          }
        }
      })
    }
  },
  onShow() {
    this.getcommunity()
  },
  getcommunity() {
    getcommunity().then(res => {
      if (_.isEqual(res.statusCode, 200)) {
        this.setData({ arealist: res.data.data })
        setStorageSyncSecond('community', res.data.data, 10)
      }
    })
  },
  areaClick() {
    this.setData({
      areashow: true,
    });
  },
  areaconfirm(e) {
    this.setData({ areashow: false, areaValue: e.detail.value.community_name, areaid: e.detail.value.uuid })
    getbuilding({ community_uuid: e.detail.value.uuid }).then(res => {
      if (_.isEqual(res.statusCode, 200)) {
        this.setData({ houselist: res.data.data, houseshow: true })
      }
    })
  },
  areacanael() { this.setData({ areashow: false }) },
  houseClick() { this.setData({ houseshow: true, }) },
  houseconfirm(e) {
    this.setData({ houseshow: false, houseValue: e.detail.value })
    getunit({ community_uuid: this.data.areaid, building_num: e.detail.value }).then(res => {
      if (_.isEqual(res.statusCode, 200)) {
        this.setData({ unitlist: res.data.data, unitshow: true })
      }
    })
  },
  housecanael() {
    this.setData({ houseshow: false })
  },
  unitClick() {
    this.setData({
      unitshow: true,
    });
  },
  unitconfirm(e) {
    this.setData({ unitshow: false, unitValue: e.detail.value })
    gethouse({ community_uuid: this.data.areaid, building_num: this.data.houseValue, unit_num: e.detail.value }).then(res => {
      if (_.isEqual(res.statusCode, 200)) {
        this.setData({ roomlist: res.data.data, roomshow: true })
      }
    })
  },
  unitcanael() {
    this.setData({ unitshow: false })
  },
  roomClick() {
    this.setData({
      roomshow: true,
    });
  },
  roomconfirm(e) {
    this.setData({ roomshow: false, roomValue: e.detail.value.house_num, roomid: e.detail.value.uuid })
  },
  roomcanael() {
    this.setData({ roomshow: false })
  },
  postuserhouse() {
    if (_.isEqual(this.data.roomid, '')) {
      wx.showToast({
        title: '请选择房屋',
        icon: 'none',
      })
    } else {
      postuserhouse({ house_uuid: this.data.roomid }).then(res => {
        console.log(res)
        if (_.isEqual(res.statusCode, 200)) {
          wx.showToast({
            title: '绑定成功',
          })
          putuser({ default_community_uuid: this.data.areaid, default_house_uuid: this.data.roomid })
          setTimeout(e => {
            wx.navigateBack({
              delta: 1,
            })
          }, 1500)
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})